using System.Collections.Generic;
using UnityEngine;

public class Susie_Follower : MonoBehaviour
{
	public enum MemberFollowerStates
	{
		Disabled,
		SettingUpPosition,
		CopyingInputs
	}

	public bool FollowingEnabled;

	public Animator SusieAnimator;

	public bool AnimationOverriden;

	public MemberFollowerStates currentState;

	[Header("1 = Susie | 1.125 = Ralsei")]
	public float delay = 0.5f;

	public Queue<Vector3> positions;

	private int frameDelay;

	public Queue<Vector2> rotations;

	private Vector3 lastPlayerPosition;

	private Vector3 lastPosition;

	private Vector3 velocity;

	private void Start()
	{
		//IL_0033: Unknown result type (might be due to invalid IL or missing references)
		//IL_0038: Unknown result type (might be due to invalid IL or missing references)
		//IL_0044: Unknown result type (might be due to invalid IL or missing references)
		//IL_0049: Unknown result type (might be due to invalid IL or missing references)
		positions = new Queue<Vector3>();
		rotations = new Queue<Vector2>();
		delay += 0.125f;
		lastPlayerPosition = ((Component)PlayerManager.Instance).transform.position;
		lastPosition = ((Component)this).transform.position;
		if (FollowingEnabled)
		{
			currentState = MemberFollowerStates.CopyingInputs;
		}
		else
		{
			currentState = MemberFollowerStates.SettingUpPosition;
		}
	}

	private void Update()
	{
		//IL_003c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0041: Unknown result type (might be due to invalid IL or missing references)
		//IL_0050: Unknown result type (might be due to invalid IL or missing references)
		//IL_0055: Unknown result type (might be due to invalid IL or missing references)
		//IL_0072: Unknown result type (might be due to invalid IL or missing references)
		//IL_0081: Unknown result type (might be due to invalid IL or missing references)
		//IL_0091: Unknown result type (might be due to invalid IL or missing references)
		frameDelay = Mathf.CeilToInt(delay * 30f);
		if (!AnimationOverriden)
		{
			AnimatePartyMember();
		}
		if (FollowingEnabled && currentState == MemberFollowerStates.SettingUpPosition)
		{
			if (Vector2.Distance(Vector2.op_Implicit(((Component)this).transform.position), Vector2.op_Implicit(((Component)PlayerManager.Instance).transform.position)) > 0.3f)
			{
				((Component)this).transform.position = Vector3.MoveTowards(((Component)this).transform.position, ((Component)PlayerManager.Instance).transform.position, 18f * Time.deltaTime);
			}
			else
			{
				currentState = MemberFollowerStates.CopyingInputs;
			}
		}
		if (!FollowingEnabled)
		{
			currentState = MemberFollowerStates.SettingUpPosition;
		}
	}

	private void FixedUpdate()
	{
		//IL_00e3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ee: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fd: Unknown result type (might be due to invalid IL or missing references)
		//IL_0109: Unknown result type (might be due to invalid IL or missing references)
		//IL_010e: Unknown result type (might be due to invalid IL or missing references)
		//IL_002c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0032: Unknown result type (might be due to invalid IL or missing references)
		//IL_004e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0063: Unknown result type (might be due to invalid IL or missing references)
		//IL_0068: Unknown result type (might be due to invalid IL or missing references)
		//IL_007d: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
		if (FollowingEnabled && currentState == MemberFollowerStates.CopyingInputs && FollowingEnabled)
		{
			if (((Component)PlayerManager.Instance).transform.position != lastPlayerPosition)
			{
				positions.Enqueue(((Component)PlayerManager.Instance).transform.position);
				lastPlayerPosition = ((Component)PlayerManager.Instance).transform.position;
				rotations.Enqueue(PlayerManager.Instance._PMove.CurrentPlayerRotation);
			}
			if (positions.Count > frameDelay)
			{
				((Component)this).transform.position = positions.Dequeue();
			}
			if (rotations.Count > frameDelay && !AnimationOverriden)
			{
				RotateSusieToDirection(rotations.Dequeue());
			}
		}
		velocity = (((Component)this).transform.position - lastPosition) / Time.fixedDeltaTime;
		lastPosition = ((Component)this).transform.position;
		SusieAnimator.SetFloat("VelocityMagnitude", ((Vector3)(ref velocity)).magnitude);
	}

	public void RotateSusieToDirection(Vector2 Dir)
	{
		//IL_000b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0021: Unknown result type (might be due to invalid IL or missing references)
		SusieAnimator.SetFloat("VelocityX", Dir.x);
		SusieAnimator.SetFloat("VelocityY", Dir.y);
	}

	public void RotateSusieTowardsPosition(Vector2 targetPosition)
	{
		//IL_0008: Unknown result type (might be due to invalid IL or missing references)
		//IL_0018: Unknown result type (might be due to invalid IL or missing references)
		//IL_0027: Unknown result type (might be due to invalid IL or missing references)
		//IL_0028: Unknown result type (might be due to invalid IL or missing references)
		//IL_0029: Unknown result type (might be due to invalid IL or missing references)
		//IL_002e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0041: Unknown result type (might be due to invalid IL or missing references)
		//IL_0057: Unknown result type (might be due to invalid IL or missing references)
		Vector2 val = default(Vector2);
		((Vector2)(ref val))._002Ector(((Component)this).transform.position.x, ((Component)this).transform.position.y);
		Vector2 val2 = targetPosition - val;
		((Vector2)(ref val2)).Normalize();
		SusieAnimator.SetFloat("VelocityX", val2.x);
		SusieAnimator.SetFloat("VelocityY", val2.y);
	}

	public void ClearAllMovementHistory()
	{
		positions.Clear();
		rotations.Clear();
	}

	private void AnimatePartyMember()
	{
		if (AnimationOverriden)
		{
			SusieAnimator.SetFloat("VelocityMagnitude", ((Vector3)(ref velocity)).magnitude);
		}
	}
}
